﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcControlsToolkit
{
    public class Pie : Plot
    {
        public Dictionary<object, double> Data { get; set; }

        /// <summary>
        /// Contructor
        /// </summary>
        /// <param name="name">Control unique name</param>
        /// <param name="data">Data</param>
        public Pie(string name, Dictionary<object, double> data): base(name)
        {
            Name = name;
            Data = data;            
            SetPlotOptions(
                new PlotOptions
                    { 
                        Type = PlotType.Pie, 
                        Shadow = true 
                    });
        }

        /// <summary>
        /// Set options for a backgound grid 
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public Pie SetGridOptions(GridOptions options){
            this.Grid = options.ToString();
            return this;
        }

        /// <summary>
        /// Set config for chart
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public Pie SetPlotOptions(PlotOptions options)
        {
            options.Type = PlotType.Pie;
            this.Options = options.ToString();
            return this;
        }

        /// <summary>
        /// Set config for legend
        /// </summary>
        /// <param name="legend"></param>
        /// <returns></returns>
        public Pie SetLegendOptions(LegendOptions legend)
        {
            this.Legend = legend.ToString();
            return this;
        }

        /// <summary>
        /// Return serialized data
        /// </summary>
        /// <returns></returns>
        protected override string GetData()
        {
            var result = new StringBuilder();
            result.Append("[");
            foreach (var key in Data.Keys)
            {
                result.Append(String.Format("[\"{0}\", {1}], ", key, Data[key]));
            }

            if (result.Length > 1)
                result.Remove(result.Length-2, 1);

            result.Append("]");
            return result.ToString();
        }
    }
}
